const express = require("express")
const router = express.Router()
const {searchHistory,addHistory,deleteHistory,updateHistory} = require("../control/history")
const {SuccessModel,ErrorModel} = require("../model/resModel")
const passport = require("passport")
const { update } = require("../control/user")

router.post("/add",passport.authenticate("jwt",{session:false}),async (req,res)=>{
  let data =await addHistory(req,res)
  console.log(data);
  if(data&&data.affectedRows>0){
    res.json(new SuccessModel("添加成功"))
  }else{
    res.json(new ErrorModel("添加失败"))
  }
  
})
router.post("/update",passport.authenticate("jwt",{session:false}),async (req,res)=>{
    let data = await updateHistory(req,res)
    console.log(data);
  if(data&&data.affectedRows>0){
    res.json(new SuccessModel("添加成功"))
  }else{
    res.json(new ErrorModel("添加失败"))
  }
})
router.post("/del",passport.authenticate("jwt",{session:false}),async (req,res)=>{
  let data =await deleteHistory(req,res)
  console.log(data);
  if(data&&data.affectedRows>0){
    res.json(new SuccessModel("删除成功"))
  }else{
    res.json(new ErrorModel("删除失败"))
  }
})
router.get("/search",async (req,res)=>{
  let data =await searchHistory(req,res)
  if(data&&data.length>0){
    res.json(new SuccessModel(data,"查找成功"))
  }else{
    res.json(new ErrorModel("查找失败"))
  }
})


module.exports=router